//
//  NSObject+PrintIvars.swift
//  表情键盘新工程-74.准备
//
//  Created by itcast on 16/5/24.
//  Copyright © 2016年 itcast. All rights reserved.
//

import Foundation

extension NSObject {
    
    class func printIvars() {
        // int a[] = {1, 2, 3};
        // a.lenght
        
        // 看不到其他的属成员变量,可以用运行时来获取私有的成员变量
        // outCount: UnsafeMutablePointer<UInt32>
        //  UnsafeMutablePointer<Ivar> // 数组中,数组名就是数组的首地址, 数组的首地址就是第一个元素的地址
        var outCount: UInt32 = 0
        let ivars = class_copyIvarList(self, &outCount)
        for var i:UInt32  = 0; i < outCount; i++ {
            let ivar = ivars[Int(i)]
            
            let cName = ivar_getName(ivar)
            
            let name = String(CString: cName, encoding: NSUTF8StringEncoding)
            print("name: \(name)")
            
            let cType = ivar_getTypeEncoding(ivar)
            let type = String(CString: cType, encoding: NSUTF8StringEncoding)
            print("type: \(type)")
        }
    }
}